package com.lai.qq.admin.modules.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.lai.qq.admin.modules.system.mapper.RoleMapper;
import com.lai.qq.admin.modules.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lai.qq.admin.modules.system.dto.input.RoleQueryPara;
import com.lai.qq.admin.modules.system.entity.Role;

/**
 * <p>
 * 系统管理-角色表 服务实现类
 * </p>
 *
 * @author: zhengqing
 * @date: 2019-08-20
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    RoleMapper roleMapper;

    @Override
    public void listPage(IPage<Role> page, RoleQueryPara filter) {
        page.setRecords(roleMapper.selectRoles(page, filter));
    }

    @Override
    public List<Role> list(RoleQueryPara filter) {
        return roleMapper.selectRoles(filter);
    }

    @Override
    public Integer saveOrUpdateData(Role para) {
        if (para.getId() != null) {
            roleMapper.updateById(para);
        } else {
            roleMapper.insert(para);
        }
        return para.getId();
    }

    /*
   根据userId获取角色
    */
    @Override
    public List<Role> getRoleByUserId(String userId) {
        return roleMapper.getRoleByUserId(userId);
    }

    @Override
    public List<String> getRoleNames(String userId) {
        List<String> roleNames = new ArrayList<>();
        List<Role> sysRoles = getRoleByUserId(userId);
        sysRoles.stream().forEach(role->{
            roleNames.add(role.getName());
        });
        return roleNames;
    }

}
